package com.zhuoyue.biz.core.api.service.impl;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;
import com.zhuoyue.common.utils.SmsTpl;

import org.apache.commons.lang3.RandomStringUtils;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import com.alibaba.fastjson.JSON;


import com.zhuoyue.biz.core.api.service.SmsService;
import com.zhuoyue.common.api.JavaSmsApi;


import static com.zhuoyue.common.exception.ErrorType.Sms_Fail_Ex;


/**
 * 短信
 *
 * @author wjming
 */
@Service("smsService")
public class SmsServiceImpl implements SmsService {

    Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private JavaSmsApi javaSmsApi;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public String sendSmsCode(String phone, String organizationCode) throws IOException {
        Object sms60 = redisTemplate.opsForValue().get(SmsTpl.rdsTpl60s(phone, organizationCode));
        if (sms60 != null && sms60 != "") {
            throw new ZyServerException(ErrorType.Service_Fail.getCode(), "每60秒只可发送1条验证码");
        }

        // 生成6位验证码，并发送手机短信进行验证
        String smsCode = RandomStringUtils.randomNumeric(6);
        try {
            redisTemplate.opsForValue().set(SmsTpl.rdsTpl(phone, organizationCode), smsCode, 5, TimeUnit.MINUTES);
            redisTemplate.opsForValue().set(SmsTpl.rdsTpl60s(phone, organizationCode), smsCode, 1, TimeUnit.MINUTES);
        } catch (Exception e) {
            throw new ZyServerException(ErrorType.Sms_Fail.getCode(), "短信验证码发送失败");
        }

        String returnStr = javaSmsApi.tplSendSms("#code#=" + smsCode, phone, organizationCode);
        Map<String, Object> map = null;
        try {
            map = (Map<String, Object>) JSON.parse(returnStr);
        } catch (RuntimeException ex) {
            redisTemplate.delete(SmsTpl.rdsTpl(phone, organizationCode));
            redisTemplate.delete(SmsTpl.rdsTpl60s(phone, organizationCode));
            throw new ZyServerException(Sms_Fail_Ex);
        }


        if (map != null) {
            switch ((Integer) map.get("code")) {
                case 53:
                    redisTemplate.delete(SmsTpl.rdsTpl(phone, organizationCode));
                    redisTemplate.delete(SmsTpl.rdsTpl60s(phone, organizationCode));
                    throw new ZyServerException(ErrorType.Sms_Fail.getCode(), "每小时内只可发送3条验证码");
                case 17:
                    throw new ZyServerException(ErrorType.Sms_Fail.getCode(), "24小时内只可发送10条验证码");
                case 33:
                    throw new ZyServerException(ErrorType.Sms_Fail.getCode(), "每30秒只可发送1条验证码");
                default:
                    break;
            }
        } else {
            throw new ZyServerException(55555, smsCode);
        }
        return smsCode;
    }

}
